package com.tetra.arena.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.tetra.arena.mysql.DataSource;


public class RankingSystem {
	public static Connection conn;
	
	DataSource database;
	ArenaSystems plugin;

	public RankingSystem(ArenaSystems instance, DataSource database){
		this.plugin = instance;
		this.database = database;
	}
	private int calcWinChance(int teamArank, int teamBrank) {
		return 1 / (1 + 10 ^ (teamBrank - teamArank) / 400);
	}

	public void updateTeamRank(int winID, int loseID, int winRank, int loseRank) {
		int winNewRank = winRank + 32 * (1 - calcWinChance(winRank, loseRank));
		int loseNewRank = winRank + 32 * (0 - calcWinChance(winRank, loseRank));
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement("UPDATE teams SET rating=? WHERE id=?");
			pst.setInt(1, winNewRank);
			pst.setInt(2, winID);
			pst.addBatch();

			pst.setInt(1, loseNewRank);
			pst.setInt(2, loseID);
			pst.addBatch();

			pst.executeBatch();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				pst.close();
			} catch (SQLException e) {
			}
		}

	}
}
